﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ShengYueWorkShop.BLL.PublicHelper
{
    /// <summary>
    /// 其他类数据
    /// </summary>
    public class OtherFun
    {
        /// <summary>
        /// 代理事件，用于返回各类数据
        /// </summary>
        /// <param name="obj"></param>
        public delegate void GetReturn(Object obj);
        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns></returns>
        public static List<string> GetLocalIPAddress()
        {
            // 获取当前的网络接口
            List<string> fStrList = new List<string>();
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    fStrList.Add(ip.ToString());
                }
            }
            return fStrList;
        }
        /// <summary>
        /// 创建新的Token
        /// </summary>
        /// <returns></returns>
        public static string CreateToken()
        {
            byte[] fTokenByte = Guid.NewGuid().ToByteArray();
            string fToken = Convert.ToBase64String(fTokenByte).TrimEnd('=');
            return fToken;
        }

        /// <summary>
        /// 按指定字符拆分bytes
        /// </summary>
        /// <param name="pInputBytes"></param>
        /// <param name="pSeparator"></param>
        /// <returns></returns>
        public static List<byte[]> BytesSplit(byte[] pInputBytes, byte pSeparator)
        {
            List<byte[]> fList = new List<byte[]>();
            List<byte> fBytes = new List<byte>();
            int fCount = 0;//只拆分前两个标识
            for (int i = 0; i < pInputBytes.Length; i++)
            {
                if (fCount < 2)
                {
                    if (pInputBytes[i] == pSeparator)
                    {
                        fList.Add(fBytes.ToArray());
                        fBytes.Clear();
                        fCount++;
                    }
                    else
                    {
                        fBytes.Add(pInputBytes[i]);
                    }
                }
                else
                {
                    fBytes.Add(pInputBytes[i]);
                }
            }
            fList.Add(fBytes.ToArray());
            return fList;
        }
        /// <summary>
        /// 按指定字符拆分bytes
        /// </summary>
        /// <param name="pInputBytes"></param>
        /// <param name="pSeparator"></param>
        /// <returns></returns>
        public static List<byte[]> BytesSplitFor1(byte[] pInputBytes, byte pSeparator)
        {
            List<byte[]> fList = new List<byte[]>();
            List<byte> fBytes = new List<byte>();
            int fCount = 0;//只拆分前两个标识
            for (int i = 0; i < pInputBytes.Length; i++)
            {
                if (fCount == 0)
                {
                    if (pInputBytes[i] == pSeparator)
                    {
                        fList.Add(fBytes.ToArray());
                        fBytes.Clear();
                        fCount++;
                    }
                    else
                    {
                        fBytes.Add(pInputBytes[i]);
                    }
                }
                else
                {
                    fBytes.Add(pInputBytes[i]);
                }
            }
            fList.Add(fBytes.ToArray());
            return fList;
        }
        /// <summary>
        /// 去除末尾的空白字符\0,0
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] BytesTrimEnd(byte[] input)
        {
            List<byte> fList = input.ToList();
            for (int i = input.Length - 1; i >= 0; i--)
            {
                if (input[i] == 0x00)
                {
                    fList.RemoveAt(i);
                }
                else
                {
                    break;
                }
            }
            return fList.ToArray();
        }
    }
}
